package com.tyqx.hashchainutil.common;

// import com.alibaba.fastjson.JSON;
import com.tyqx.hashchainbean.module.HashIndex;
import com.tyqx.hashchainutil.org.app.util.Util;
import org.json.JSONObject;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class HashValueUtil {

    public static HashIndex getHashValue(String json) throws Exception {
        String caType = "msp";
        Long orgIndex = 0L;
        JSONObject jsonObject=new JSONObject(json);
        jsonObject = JsonUtil1.getSortJson(jsonObject);
        String userName = jsonObject.getString("userName");

        Path keyPath = (Path) Util.readUserContext(caType, userName, orgIndex).get("clientKeyPath");
        ChainCode chainCode=new ChainCode();
        Map<String,String> hashIndexMap=chainCode.insertHash(userName, jsonObject, keyPath,"insertHashItem");

        HashIndex hashIndex=new HashIndex();
        hashIndex.setHashValue(hashIndexMap.get("hashValue"));
        hashIndex.setTxId(hashIndexMap.get("txid"));
        hashIndex.setBlockNum(Long.valueOf(hashIndexMap.get("blockNum")));
        SimpleDateFormat format =  new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );

        Long time=new Long(hashIndexMap.get("timeStamp"));

        String d = format.format(time);

        Date date=format.parse(d);

        hashIndex.setTxTimeStamp(date);
        return hashIndex;
    }

    public static HashIndex getHashValue(String json,MongoTemplate mongoTemplate) throws Exception {
        String caType = "msp";
        Long orgIndex = 0L;
        JSONObject jsonObject=new JSONObject(json);
        jsonObject = JsonUtil1.getSortJson(jsonObject);
        String userName = jsonObject.getString("userName");

        Path keyPath = (Path) Util.readUserContext(caType, userName, orgIndex).get("clientKeyPath");
        ChainCode chainCode=new ChainCode();
        Map<String,String> hashIndexMap=chainCode.insertHash(userName, jsonObject, keyPath,"insertHashItem",mongoTemplate);

        HashIndex hashIndex=new HashIndex();
        hashIndex.setHashValue(hashIndexMap.get("hashValue"));
        hashIndex.setTxId(hashIndexMap.get("txid"));
        hashIndex.setBlockNum(Long.valueOf(hashIndexMap.get("blockNum")));
        SimpleDateFormat format =  new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );

        Long time=new Long(hashIndexMap.get("timeStamp"));

        String d = format.format(time);

        Date date=format.parse(d);

        hashIndex.setTxTimeStamp(date);
        return hashIndex;
    }


    public static HashIndex insertHashValue(String rootHashValue, String userName, MongoTemplate mongoTemplate) throws Exception {
        String caType = "msp";
        Long orgIndex = 0L;
        Path keyPath = (Path) Util.readUserContext(caType, userName, orgIndex).get("clientKeyPath");
        ChainCode chainCode=new ChainCode();
        Map<String,String> hashIndexMap=chainCode.insertHashValue(userName, rootHashValue, keyPath,"insertHashItem",mongoTemplate);

        HashIndex hashIndex=new HashIndex();
        hashIndex.setHashValue(hashIndexMap.get("hashValue"));
        hashIndex.setTxId(hashIndexMap.get("txid"));
        hashIndex.setBlockNum(Long.valueOf(hashIndexMap.get("blockNum")));
        SimpleDateFormat format =  new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );

        Long time=new Long(hashIndexMap.get("timeStamp"));

        String d = format.format(time);

        Date date=format.parse(d);

        hashIndex.setTxTimeStamp(date);
        return hashIndex;
    }

    public static HashIndex insertHashValue(String rootHashValue, String userName) throws Exception {
        String caType = "msp";
        Long orgIndex = 0L;
        Path keyPath = (Path) Util.readUserContext(caType, userName, orgIndex).get("clientKeyPath");
        ChainCode chainCode=new ChainCode();
        Map<String,String> hashIndexMap=chainCode.insertHashValue(userName, rootHashValue, keyPath,"insertHashItem");

        HashIndex hashIndex=new HashIndex();
        hashIndex.setHashValue(hashIndexMap.get("hashValue"));
        hashIndex.setTxId(hashIndexMap.get("txid"));
        hashIndex.setBlockNum(Long.valueOf(hashIndexMap.get("blockNum")));
        SimpleDateFormat format =  new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );

        Long time=new Long(hashIndexMap.get("timeStamp"));

        String d = format.format(time);

        Date date=format.parse(d);

        hashIndex.setTxTimeStamp(date);
        return hashIndex;
    }

    public static  boolean flagKeyPath(String json)throws Exception{
        String caType = "msp";
        Long orgIndex = 0L;
        JSONObject jsonObject=new JSONObject(json);
        String userName = jsonObject.getString("userName");
        Path keyPath = (Path) Util.readUserContext(caType, userName, orgIndex).get("clientKeyPath");
        if(EmptyUtils1.isEmpty(keyPath)){
            return false;
        }
        return true;
    }
}
